package name.marz.to.go.ahg.core.executer;

import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.logging.Logger;

import name.marz.to.go.ahg.core.AutoGetterHttpClient;
import name.marz.to.go.ahg.core.checker.DocumentChecker;
import name.marz.to.go.ahg.core.exception.UserProcessException;
import name.marz.to.go.ahg.core.responsehandler.DocumentResponseHandler;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.jsoup.nodes.Document;


public abstract class AbstractLoginExecuter extends AbstractPostExecuter<Document> {
	private static final Logger log = Logger.getLogger(AbstractLoginExecuter.class.getName());
	private final ResponseHandler<Document> handler;
	
	protected String[] userIdentifiers;
	protected String userPass;
	{
		super.setPageName("ログイン要求");
	}
	
	public AbstractLoginExecuter(URI uri, AutoGetterHttpClient client) {
		super(uri, client);
		this.handler = new DocumentResponseHandler(client.getDefaultCharset());
	}

	public AbstractLoginExecuter(URI uri, AutoGetterHttpClient client, String[] userIds,String pass) {
		super(uri, client);
		this.setUserIdentifiers(userIds);
		this.setUserPass(pass);
		this.handler = new DocumentResponseHandler(client.getDefaultCharset());
	}
	
	@Override
	public Document execute() throws ClientProtocolException, IOException,UserProcessException {
		Document doc = super.execute();
		log.finer("response source="+doc.html());
		// ログインチェック
		DocumentChecker checker = getChecker();
		if ( checker != null ) checker.check(doc);
		
		return doc;
	}
	protected abstract List<NameValuePair> getRequestParameters();
	protected abstract DocumentChecker getChecker();
	
	public void setUserIdentifiers( String[] identifiers ) {
		this.userIdentifiers = identifiers;
	}
	public String[] getUserIdentifiers() {
		return userIdentifiers;
	}
	public String getUserPass() {
		return userPass;
	}
	public void setUserPass( String pass ) {
		this.userPass = pass;
	}

	@Override
	protected ResponseHandler<Document> getResponseHandler() {
		return handler;
	}
	
}
